home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- #
- # utils.c
- #
- # This segment handles file parsing, and basic utility functions.
- #
- # Author(s): Michael Marinkovich
- # Apple Developer Technical Support
- # marink@apple.com
- #
- # Modification History:
- #
- # 4/3/96 MWM Initial coding
- #
- # Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
- #
- #
- # You may incorporate this sample code into your applications without
- # restriction, though the sample code has been provided "AS IS" and the
- # responsibility for its operation is 100% yours. However, what you are
- # not permitted to do is to redistribute the source as "DSC Sample Code"
- # after having made changes. If you're going to re-distribute the source,
- # we require that you make it clear in the source that the code was
- # descended from Apple Sample Code, but that you've made changes.
- #
- *************************************************************************************/
-
- #include <Events.h>
- #include <Finder.h>
- #include <ToolUtils.h>
- #include <Gestalt.h>
- #include <OSUtils.h>
- #include <StandardFile.h>
-
- #include "App.h"
- #include "Proto.h"
-
- //----------------------------------------------------------------------
- // Globals
- //----------------------------------------------------------------------
-
- OSType gFileType;
-
-
- //----------------------------------------------------------------------
- //
- // DoOpenNew - query user for new file. Open it and
- // display in a new window.
- //
- //----------------------------------------------------------------------
-
- void DoOpenNew(void)
- {
- OSErr err = noErr;
- StandardFileReply reply;
- SFTypeList typeList;
- DlgHookYDUPP theDialogHookUPP;
- FileFilterYDUPP theDialogFileFilterUPP;
- WindowRef window;
- GWorldPtr theWorld = nil;
- Rect bounds;
- Point thePt = {-1, -1};
- short numTypes;
- DocHnd doc;
-
- gFileType = kAllType;
-
- theDialogHookUPP = NewDlgHookYDProc(CustomGetFileHook);
- theDialogFileFilterUPP = NewFileFilterYDProc(CustomFileDlogFilter);
-
- CustomGetFile(theDialogFileFilterUPP, -1,
- typeList, &reply, 132,
- thePt, theDialogHookUPP,
- nil,nil,nil,nil);
-
- DisposeRoutineDescriptor((UniversalProcPtr)theDialogHookUPP);
- DisposeRoutineDescriptor((UniversalProcPtr)theDialogFileFilterUPP);
-
- if ( reply.sfGood )
- {
- switch (reply.sfType)
- {
- case kJPEGType:
- theWorld = OpenJPEGFile(reply.sfFile, &err);
- break;
-
- case kPICTType:
- err = OpenPictFile(&reply.sfFile, &theWorld);
- break;
-
- }
-
- if (err == noErr && theWorld != nil)
- {
- bounds = theWorld->portRect;
- SetRect(&bounds, 3, GetMBarHeight() + kTitleBarHeight + kFudgeFactor,
- bounds.right + 3, bounds.bottom + GetMBarHeight()
- + kTitleBarHeight + kFudgeFactor);
-
- window = CreateWindow(nil, nil, &bounds, reply.sfFile.name, false,
- zoomDocProc, kDocKind, (WindowPtr)-1, true, nil );
-
- if (window != nil)
- {
- doc = (DocHnd)GetWRefCon(window);
- if (doc != nil)
- (**doc).world = theWorld;
- (**doc).fileType = reply.sfType;
- }
- }
- }
-
-
- if (err != noErr)
- HandleError(err, false);
-
- }
-
-
-
- //----------------------------------------------------------------------
- //
- // DoSaveImage - main entry point for saving an image
- //
- //
- //----------------------------------------------------------------------
-
- OSErr DoSaveImage(WindowRef window)
- {
- OSErr err = noErr;
- StandardFileReply reply;
- SFTypeList typeList;
- DlgHookYDUPP theDialogHookUPP;
- Point thePt = {-1, -1};
- short numTypes;
- Str255 title;
- DocHnd doc;
-
-
- doc = (DocHnd)GetWRefCon(window);
- if (doc != nil)
- {
- gFileType = (**doc).fileType;
- GetWTitle(window, title);
-
- theDialogHookUPP = NewDlgHookYDProc(CustomPutFileHook);
-
- CustomPutFile((ConstStr255Param)"", title,
- &reply, 133, thePt, theDialogHookUPP,
- nil, nil, nil, nil);
-
- DisposeRoutineDescriptor((UniversalProcPtr)theDialogHookUPP);
-
- SetCursor(*GetCursor(watchCursor));
-
- if (reply.sfGood)
- {
- if (reply.sfReplacing)
- err = FSpDelete(&reply.sfFile);
-
- if (err == noErr)
- {
- switch (gFileType)
- {
- case kJPEGType:
- err = SaveJPEGFile(reply, window);
- if (err == noErr)
- (**doc).dirty = true;
- break;
-
- case kPICTType:
- err = SavePictFile(&reply.sfFile, (**doc).world);
- if (err == noErr)
- (**doc).dirty = true;
- break;
-
- }
- }
-
- }
- else
- if (err == userCanceledErr)
- err = noErr;
-
- SetCursor(&qd.arrow);
- }
-
- return err;
- }
-
-
- //----------------------------------------------------------------------
- //
- // CustomFileDlogFilter -
- //
- //
- //----------------------------------------------------------------------
-
- pascal Boolean CustomFileDlogFilter(CInfoPBPtr theParamBlok, Ptr theDataPtr)
- {
- Boolean notDisplayed = true;
-
- switch (theParamBlok->hFileInfo.ioFlFndrInfo.fdType)
- {
- case kPICTType:
- if (gFileType == kPICTType || gFileType == kAllType)
- notDisplayed = false;
- break;
-
- case kJPEGType:
- if (gFileType == kJPEGType || gFileType == kAllType)
- notDisplayed = false;
- break;
-
- default:
- if ((theParamBlok->dirInfo.ioFlAttrib & ioDirMask) &&
- !(theParamBlok->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible))
- notDisplayed = false;
- break;
- }
-
-
- return notDisplayed;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // CustomGetFileHook -
- //
- //
- //----------------------------------------------------------------------
-
- pascal short CustomGetFileHook(short item, DialogPtr theDlogPtr, void* theData)
- {
- Handle theHandle;
- Rect theRect;
- short theType;
- short ignored;
-
- switch (item)
- {
- case sfHookFirstCall:
- GetDItem(theDlogPtr, rCustomGetPopupID, &theType, &theHandle, &theRect);
-
- switch (gFileType)
- {
- case kAllType:
- theType = kAllGetID;
- break;
-
- case kPICTType:
- theType = kPICTGetID;
- break;
-
- case kJPEGType:
- theType = kJPEGGetID;
- break;
-
- }
-
- SetCtlValue((ControlHandle)theHandle,theType);
- item = sfHookNullEvent;
- break;
-
- case rCustomGetPopupID:
- GetDItem(theDlogPtr, item, &ignored, &theHandle, &theRect);
- theType = GetCtlValue((ControlHandle)theHandle);
-
- switch (theType)
- {
- case kAllGetID:
- gFileType = kAllType;
- item = sfHookRebuildList;
- break;
-
- case kPICTGetID:
- gFileType = kPICTType;
- item = sfHookRebuildList;
- break;
-
- case kJPEGGetID:
- gFileType = kJPEGType;
- item = sfHookRebuildList;
- break;
-
- }
- break;
- }
-
- return item;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // CustomPutFileHook - used to sort file types when saving a file
- //
- //
- //----------------------------------------------------------------------
-
- pascal short CustomPutFileHook(short item,DialogPtr theDlogPtr,void* theData)
- {
- Handle theHandle;
- Rect theRect;
- short theType;
- short ignored;
-
- if (GetWRefCon(theDlogPtr) != sfMainDialogRefCon)
- return item;
-
- switch (item)
- {
- case sfHookFirstCall:
- GetDItem(theDlogPtr, rCustomPutPopupID, &theType, &theHandle, &theRect);
-
- switch (gFileType)
- {
- case kPICTType:
- theType = kPICTPutID;
- break;
-
- case kJPEGType:
- theType = kJPEGPutID;
- break;
- }
-
- SetCtlValue((ControlHandle)theHandle,theType);
- item = sfHookNullEvent;
- break;
-
- case rCustomPutPopupID:
- GetDItem(theDlogPtr,item,&ignored,&theHandle,&theRect);
- theType = GetCtlValue((ControlHandle)theHandle);
-
- switch (theType)
- {
- case kPICTPutID:
- gFileType = kPICTType;
- break;
-
- case kJPEGPutID:
- gFileType = kJPEGType;
- break;
- }
-
- item = sfHookNullEvent;
- break;
- }
-
- return item;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // ZeroRect - zero the top-left points of a rect
- //
- //
- //----------------------------------------------------------------------
-
- void ZeroRect(Rect *r)
- {
- Rect zr;
-
- zr = *r;
-
- if (zr.left < 0)
- OffsetRect(&zr,zr.left,0);
- if (zr.top < 0)
- OffsetRect(&zr,0,zr.top);
-
- OffsetRect(&zr,-zr.left,-zr.top);
-
- *r = zr;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // pstrcpy - pascal string copy
- //
- //
- //----------------------------------------------------------------------
-
- void pstrcpy(StringPtr dst, StringPtr src)
- {
- short c;
-
- for (c = *src; c > -1; c--)
- dst[c] = src[c];
- }
-
-
- //----------------------------------------------------------------------
- //
- // pstrcat - pascal string concat
- //
- //
- //----------------------------------------------------------------------
-
- void pstrcat(StringPtr dst, StringPtr src)
- {
-
- short c;
-
- for (c = 1; c < src[0] + 1; c++)
- dst[dst[0] + c] = src[c];
- dst[0] += src[0];
- }
-